<?php

namespace App\Store\Controllers\Wxapp\WxappGift;

use App\Common\Services\Store\Common as StoreCommon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Wxapp\Gift\WxappGiftAllot;
use App\Store\Models\Wxapp\Gift\WxappGiftLog;
use Illuminate\Support\Facades\DB;
use App\Store\Models\Wxapp\Gift\WxappGiftStocklist as WxappGiftStock;

/**
 * 礼品调拨列表
 */
class WxappGiftAllotController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = WxappGiftAllot::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }


     /**
     * 调拨审核
     */
    public function doauditsuccess(Request $request)
    {
        $id = $request->id ?? 0;
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxappGiftAllot::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        if ($info->audit_status != 1) {
            return response()->json(['code' => '001', 'msg' => '该入库单已审核']);
        }
        DB::beginTransaction();
        try {
            WxappGiftAllot::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->update(['audit_status'=>2,'audit_at'=>time()]);

            //审核后入库
            $stockinfo=WxappGiftStock::where(['cp_id' => $cp_id,'store_id' => $info->store_id,'id' => $info->gift_id])->first();

            $targetstockinfo=WxappGiftStock::where(['cp_id' => $cp_id,'store_id' => $info->target_store_id,'gift_id' => $stockinfo->gift_id])->first();




            if($targetstockinfo){
                
                WxappGiftStock::where(['cp_id' => $cp_id,'store_id' => $info->target_store_id,'gift_id' => $stockinfo->gift_id])
                ->update(['total_count'=>DB::raw('total_count + ' . $info->total_count),'current_count'=>DB::raw('current_count + ' . $info->total_count)]);

            }else{
             
                WxappGiftStock::create(
                    [
                    'cp_id' => $cp_id,
                    'store_id' => $info->target_store_id,
                    'sendgoods_name'=>$stockinfo->sendgoods_name,
                    'gift_category_id' => $stockinfo->gift_category_id,
                    'gift_id'=>$stockinfo->gift_id,
                    'gift_name'=>$stockinfo->gift_name,
                    'total_count'=>$info->total_count,
                    'current_count'=>$info->total_count,
                    'pieceprice'=>$stockinfo->pieceprice,
                    'price'=>$stockinfo->price,
                    'need_points'=>$stockinfo->need_points,
                    'piecelimit'=>$stockinfo->piecelimit,
                    'valid_type'=>$stockinfo->valid_type,
                    'valid_num'=>$stockinfo->valid_num,
                    'valid_end_at'=>$stockinfo->valid_end_at,
                    'gift_img'=>$stockinfo->gift_img,
                    'desc'=>$stockinfo->desc,
                    'gift_img'=>$stockinfo->gift_img,
                    'remark'=>$info->remark,
                    'audit_status'=>2,
                    'create_at'=>time(),
                    'update_at'=>time(),
                    'create_account_id'=>$accountinfo['id'],
                    'create_account_real_name'=>$accountinfo['real_name'],
                    'update_account_id'=>$accountinfo['id'],
                    'update_account_real_name'=>$accountinfo['real_name'],
                    ]
                );
            }
            //增加流水记录
            $data=[
                'cp_id'=>$cp_id,
                'gift_id'=>$info->id,
                'gift_name'=>$info->gift_name,
                'count'=>$info->total_count,
                'type'=>3,
                'store_id'=>$info->target_store_id,
                'create_account_id'=>$accountinfo['id'],
                'create_account_real_name'=>$accountinfo['real_name'],
                'update_account_id'=>$accountinfo['id'],
                'update_account_real_name'=>$accountinfo['real_name'],
                'create_at'=>time(),
                'update_at'=>time(),
                'remark'=>$info->remark,
                'start_store_id'=>$info->store_id,
                'target_store_id'=>$info->target_store_id,
            ];
            WxappGiftLog::create($data); 
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json(['code' => '001', 'msg' => '审核失败' . $e->getMessage()]);
        }

        return response()->json(['code' => '000', 'msg' => '调拨单审核成功']);
    }

    /**
     * 调拨审核取消
     */
    public function doauditcancel(Request $request)
    {
        $id = $request->id ?? 0;
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $info = WxappGiftAllot::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        if ($info->audit_status != 1) {
            return response()->json(['code' => '001', 'msg' => '该调拨单已审核']);
        }
        WxappGiftAllot::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->update(['audit_status'=>3,'audit_at'=>time()]);

        WxappGiftStock::where(['cp_id' => $cp_id,'store_id' => $info->store_id,'id' => $info->gift_id])
                ->update(['current_count'=>DB::raw('current_count + ' . $info->total_count)]);//恢复调拨库存

        return response()->json(['code' => '000', 'msg' => '调拨审核取消成功']);
    }




}